package com.tian.dataStructure.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode2 heroNode1=new HeroNode2(1,"hello","world");
        HeroNode2 heroNode2=new HeroNode2(2,"田","小田");
        HeroNode2 heroNode3=new HeroNode2(3,"亚","ya");
        HeroNode2 heroNode4=new HeroNode2(4,"康","kang");

        DoubleLinkedList list=new DoubleLinkedList();
        list.add(heroNode2);
        list.add(heroNode1);
        list.add(heroNode3);
        list.add(heroNode4);
        list.list();

        //修改
        HeroNode2 heroNode5=new HeroNode2(4,"康康康","kang");
        list.update(heroNode5);
        System.out.println("修改后的链表的情况");
        list.list();

        //删除
        list.del(4);
        System.out.println("删除后的链表情况");
        list.list();


    }
}

class DoubleLinkedList {
    //初始化一个头节点，不存放具体数据
    private HeroNode2 head = new HeroNode2(0, "", "");

    //返回头节点
    public HeroNode2 getHead() {
        return head;
    }

    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }

        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    public void add(HeroNode2 heroNode2) {
        HeroNode2 temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode2;
        heroNode2.pre = temp;
    }

    public void update(HeroNode2 heroNode2) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == heroNode2.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = heroNode2.name;
            temp.nickName = heroNode2.nickName;
        } else {
            System.out.printf("没有找到编号 %d 的节点，不能修改\n", heroNode2.no);
        }
    }

    public void del(int no){
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp=head.next;
        boolean flag=false;
        while (true){
            if (temp==null){
                break;
            }
            if (temp.no==no){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if (flag) {
            temp.pre.next=temp.next;
            //这里注意，如果是最后一个节点，就不需要执行下面这句话，否则空指针
            if (temp.next!=null){
                temp.next.pre=temp.pre;
            }
        }else {
            System.out.printf("要删除的 %d 节点不存在\n",no);
        }
    }

}

class HeroNode2{
    public int no;
    public String name;
    public String nickName;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }



    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}